package com.service;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;
import org.hibernate.LockMode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import com.dao.IRetBoxDao;
import com.dao.IRetPrdInfoDao;
import com.dao.IRetPrdOutDao;
import com.dao.IBisPathDao;
import com.dao.IBisOpeDao;
import com.dao.IRetWODao;
import com.icim.service.ICIMBaseService;
import com.model.Ret_box;
import com.model.Ret_prd_info;
import com.model.Ret_prd_out;
import com.model.Bis_ope;
import com.model.Bis_path;
import com.model.Ret_wo;
import com.rtnBean.NotFoundRtnBeen;
import com.rtnBean.SimpleRtnBeen;
import com.tx.Xppnlout.XppnloutI;
import com.tx.Xppnlout.XppnloutO;
import com.util.GUIDGenerator;
import com.util.MqOpe;
import com.util.Util;

import static com.def.GenericDef.*;

@Scope("prototype")
@Service("XppnloutService")
public class XppnloutService extends ICIMBaseService{
	private static Logger logger = Logger.getLogger(XppnloutService.class);
	private static String TX_NAME = T_XPPNLOUT;

	private XppnloutI inTrx;
	private XppnloutO outTrx;
	private MqOpe mqOpe;

	private String gszMsgBuf = "";
	private Timestamp gEvtTimestamp;
	private Double gEvtTimeD;
	private String gEvtSeqID;

	@Autowired
	private IRetBoxDao retBoxDao;
	@Autowired
	private IRetWODao retWoDao;
	@Autowired
	private IRetPrdInfoDao retPrdInfoDao;

	@Autowired
	private IRetPrdOutDao retPrdOutDao;
	@Autowired
	private IBisPathDao bispathDao;
	@Autowired
	private IBisOpeDao bisOpeDao;

	public String subMainProc(String strInMsg) {
		long rtnCode = _NORMAL;

		String outMsg = null;
		gszMsgBuf = "";
		gEvtTimestamp = Util.getCurrentTimestamp();
		gEvtTimeD = Util.getDoubleTime();
		gEvtSeqID = GUIDGenerator.javaGUID();
		inTrx = new XppnloutI();
		outTrx = new XppnloutO();
		outTrx.setTrx_id(TX_NAME);
		outTrx.setType_id(TRX_OUT);
		outTrx.setRtn_code(RETURN_CODE_SET_ERROR);
		mqOpe = new MqOpe();

		try {
			inTrx = (XppnloutI) mqOpe.inStr2Obj(strInMsg, inTrx);
			logger.info("InTrx is " + strInMsg);

			rtnCode = subMainProc2();
			if (rtnCode != _NORMAL) {
				TransactionAspectSupport.currentTransactionStatus()
						.setRollbackOnly();
				outTrx.setRtn_code(String.valueOf(rtnCode));
				outTrx.setRtn_mesg(gszMsgBuf);
			} else {
				outTrx.setRtn_code(String.valueOf(RETURN_CODE_OK));
				outTrx.setRtn_mesg(RETURN_MESG_OK);
				retPrdInfoDao.flushCurSession();
			}
		} catch (Exception ex) {
			TransactionAspectSupport.currentTransactionStatus()
					.setRollbackOnly();
			outTrx.setRtn_mesg(ex.toString());
			outTrx.setRtn_code(RETURN_CODE_UNKNOWN);
			logger.error(Util.stackTraceToString(ex));
		} finally {
			outMsg = mqOpe.obj2Str(outTrx);
			return outMsg;
		}
	}

	/**
	 * Check inTrx items
	 * 
	 * @return
	 */
	private long checkInTtx() {
		long rtnCode = _NORMAL;

		if (Util.isSpaceCheck(inTrx.getBox_id())) {
			gszMsgBuf = "Box ID can't be null![" + inTrx.getBox_id() + "]";
			return E_XPMOVEOUT_INPUT_ITEM_EMPT;
		}
		if (Util.isSpaceCheck(inTrx.getEvt_usr())) {
			gszMsgBuf = "User ID can't be null![" + inTrx.getEvt_usr() + "]";
			return E_XPMOVEOUT_INPUT_ITEM_EMPT;
		}
		if (null == inTrx.getPnl_cnt() || 0 >= inTrx.getPnl_cnt()) {
			gszMsgBuf = "Panel count [" + inTrx.getPnl_cnt() + "] is invalied!";
			return E_XPMOVEOUT_INPUT_ITEM_EMPT;
		}

		return rtnCode;
	}

	//华星原箱包装返回


	/**
	 * All move in logic
	 * 
	 * @return
	 */
	private long subMainProc2() {
		long rtnCode = _NORMAL;
		SimpleRtnBeen rtnBeen = new SimpleRtnBeen(_NORMAL);
		Ret_box ret_box = new Ret_box();
		List<Ret_prd_info> gRet_prd_infoList = new ArrayList<Ret_prd_info>();

		// Check inTrx items
		rtnCode = checkInTtx();
		if (rtnCode != _NORMAL) {
			return rtnCode;
		}

		// Check box
		rtnBeen = checkRetBox(inTrx.getBox_id());
		if (rtnBeen.getlRc() != _NORMAL) {
			return rtnBeen.getlRc();
		} else {
			ret_box = (Ret_box) rtnBeen.getObj();
		}
		
		//Check box inpr QA&pack
//		rtnCode = checkBoxforQAandpack();
//		if (rtnCode != _NORMAL) {
//			return rtnCode;
//		}

		// Check sht & Save judge info
		rtnBeen = checkAndSetRetPrdInfo(ret_box);
		if (rtnBeen.getlRc() != _NORMAL) {
			return rtnBeen.getlRc();
		} else {
			gRet_prd_infoList = (List<Ret_prd_info>) rtnBeen.getObj();
		}

		// Update prd info -> into box
		rtnBeen = updateRetPrdInfoAndBoxInfo(gRet_prd_infoList, ret_box);
		if (rtnBeen.getlRc() != _NORMAL) {
			return rtnBeen.getlRc();
		}
		
		rtnCode = checkBoxMix();
		if (rtnCode != _NORMAL) {
			return rtnCode;
		}
		return rtnCode;
	}

	private long checkBoxforQAandpack() {
		long rtnCode = _NORMAL;
		if(inTrx.getInflg().equals("A")){
		        List<Ret_prd_info> prd_list = new ArrayList<Ret_prd_info>();
		        String hql = "FROM Ret_prd_info WHERE pv_box_id_fk='"+inTrx.getBox_id()+"' AND prd_stat='INPR' AND cr_proc_id_fk IN ('DMQA','JBPK') ";
		        prd_list = retPrdInfoDao.find(hql);
		        if(prd_list.size()>0){
		        	gszMsgBuf="箱子["+inTrx.getBox_id()+"]已经在QA或PACK站入账,请勿再用其转入玻璃!";
			        return E_XPPNLOUT_BOX_INVALIED;
		        }
		}else if(inTrx.getInflg().equals("B")){
	        List<Ret_prd_info> prd_list = new ArrayList<Ret_prd_info>();
	        String hql = "FROM Ret_prd_info WHERE pv_box_id_fk='"+inTrx.getBox_id()+"' AND prd_stat='INPR' AND cr_proc_id_fk IN ('DMQA','JBQA') ";
	        prd_list = retPrdInfoDao.find(hql);
	        if(prd_list.size()>0){
	        	gszMsgBuf="箱子["+inTrx.getBox_id()+"]已经在QA或PACK站入账,请勿再用其转入玻璃!";
		        return E_XPPNLOUT_BOX_INVALIED;
	        }
		}else if(inTrx.getInflg().equals("C")){
	        List<Ret_prd_info> prd_list = new ArrayList<Ret_prd_info>();
	        String hql = "FROM Ret_prd_info WHERE pv_box_id_fk='"+inTrx.getBox_id()+"' AND prd_stat='INPR' AND cr_proc_id_fk IN ('JBPK','JBQA') ";
	        prd_list = retPrdInfoDao.find(hql);
	        if(prd_list.size()>0){
	        	gszMsgBuf="箱子["+inTrx.getBox_id()+"]已经在QA或PACK站入账,请勿再用其转入玻璃!";
		        return E_XPPNLOUT_BOX_INVALIED;
	        }
		}else if(inTrx.getInflg().equals("D")){
	        List<Ret_prd_info> prd_list = new ArrayList<Ret_prd_info>();
	        String hql = "FROM Ret_prd_info WHERE pv_box_id_fk='"+inTrx.getBox_id()+"' AND prd_stat='INPR' AND cr_proc_id_fk IN ('JBPK','JBQA','DMQA') ";
	        prd_list = retPrdInfoDao.find(hql);
	        if(prd_list.size()>0){
	        	gszMsgBuf="箱子["+inTrx.getBox_id()+"]已经在QA或PACK站入账,请勿再用其转入玻璃!";
		        return E_XPPNLOUT_BOX_INVALIED;
	        }
		}
		return rtnCode;
	}

	/**
	 * Update prd info -> into box
	 * 
	 * @return
	 */
	private SimpleRtnBeen updateRetPrdInfoAndBoxInfo(
			List<Ret_prd_info> ret_prd_infoList, Ret_box ret_box) {
		if (ret_prd_infoList.isEmpty()) {
			gszMsgBuf = "No prd in inMsg, should not happen!";
			return new SimpleRtnBeen(E_XPPNLOUT_PRD_LIST_NULL);
		}

		ret_box.setPrd_qty(ret_box.getPrd_qty() + ret_prd_infoList.size());
		if (1 <= ret_box.getPrd_qty()) {
			ret_box.setReal_emp(EXIST_CARRIER);
		}
		ret_box.setWo_id_fk(ret_prd_infoList.get(0).getWo_id_fk());
		ret_box.setMdl_id_fk(ret_prd_infoList.get(0).getMdl_id_fk());
		ret_box.setMdl_typ_fk(ret_prd_infoList.get(0).getMdl_typ_fk());
		ret_box.setMdl_cate_fk(ret_prd_infoList.get(0).getMdl_cate_fk());
		ret_box.setGroup_id(ret_prd_infoList.get(0).getGroup_id());//修改箱子的组代码
		retBoxDao.update(ret_box);
		retPrdInfoDao.saveOrUpdateAll(ret_prd_infoList);
		outTrx.setWo_id(ret_prd_infoList.get(0).getWo_id_fk());

		outTrx.setBox_id(ret_box.getBox_id());
		outTrx.setPrd_qty(ret_box.getPrd_qty().toString());
		outTrx.setEnd_time(gEvtTimestamp);
		outTrx.setEvt_user(inTrx.getEvt_usr());
		outTrx.setTool_id(inTrx.getTool_id());
		outTrx.setEvt_timestamp(gEvtTimestamp);

		return new SimpleRtnBeen(_NORMAL);
	}

	/**
	 * Get & chk & set prd info
	 * 
	 * @return
	 */
	private SimpleRtnBeen checkAndSetRetPrdInfo(Ret_box ret_box) {
		List<Ret_prd_info> prdList = new ArrayList<Ret_prd_info>();
		int pnlCnt = inTrx.getPnl_cnt();
		if (0 == pnlCnt) {
			gszMsgBuf = "转入产品数量为 0!";
			return new SimpleRtnBeen(E_XPPNLOUT_PRD_CNT_INVALIED);
		}
		String sql = "FROM Ret_prd_info WHERE prd_seq_id in ("+ inTrx.getPrd_ids() +")";
		prdList = retPrdInfoDao.find(sql);
		if(prdList.size() != pnlCnt){
			gszMsgBuf = "部分产品代码不存在，请刷新界面！";
			return new SimpleRtnBeen(E_XPPNLOUT_INVALID_CNT);
		}
		for(int i=0; i < pnlCnt; i++){
			// Must INPR
			if (!PROC_IN_PROCESS.equals(prdList.get(i).getPrd_stat())) {
				gszMsgBuf = "状态为 [" + prdList.get(i).getPrd_stat() + "] ,必须为 ["
						+ PROC_IN_PROCESS + "]";
				return new SimpleRtnBeen(E_XPPNLOUT_INVALIED_PRD_STAT);
			}
			// Box ID must space
			if (!Util.isSpaceCheck(prdList.get(i).getBox_id_fk())) {
				gszMsgBuf = "产品 [" + prdList.get(i).getPrd_seq_id() + "] 已经转入 ["
						+ prdList.get(i).getBox_id_fk() + "]";
				return new SimpleRtnBeen(E_XPPNLOUT_PRD_ALREADY_IN_BOX);
			}
			if("3".equals(prdList.get(i).getBnk_flg())){
				gszMsgBuf = "产品 [" + prdList.get(i).getPrd_seq_id() + "] 已经在制保留";
				return new SimpleRtnBeen(E_XPPNLOUT_PRD_ALREADY_IN_BOX);
			}
	//		if(inTrx.getWoflg().equals("N")){   //  原箱回和非原箱回去  直接按照转入顺序赋值
				String slot_no = getSlotNo();
				if(pnlCnt==1){
			    prdList.get(i).setSlot_no(slot_no);
			    outTrx.setSlot_no(slot_no);
			  }else if(pnlCnt>1){
			  int slot =Integer.parseInt(slot_no)+i;
			  prdList.get(i).setSlot_no(slot+"");
	       		outTrx.setSlot_no(String.valueOf(slot));
			  }
		//	}
			prdList.get(i).setTool_id_fk(inTrx.getTool_id());
			prdList.get(i).setCr_tool_id_fk(inTrx.getTool_id());
			prdList.get(i).setBox_id_fk(inTrx.getBox_id());
			prdList.get(i).setP2_timestamp(gEvtTimestamp);
			prdList.get(i).setEvt_usr(inTrx.getEvt_usr());
			prdList.get(i).setEvt_timestamp(gEvtTimestamp);
			prdList.get(i).setEvt_time_d(gEvtTimeD);
			prdList.get(i).setEvt_seq_id(gEvtSeqID);
			
			for(int j=0; j<pnlCnt; j++){
				if(prdList.get(i).getPrd_seq_id().equals(inTrx.getIary().get(j).getPrd_seq_id())){
					if (prdList.get(i).getCr_proc_id_fk().equals("JBPG") || prdList.get(i).getCr_proc_id_fk().equals("JBYZ")
						|| prdList.get(i).getCr_proc_id_fk().equals("PGGY")) {
						// Save buff tool & time
						if (!Util.isSpaceCheck(inTrx.getIary().get(j)
								.getTft_buff_time())) {
							prdList.get(i).setT_rubbing_time(Integer.valueOf(inTrx
									.getIary().get(j).getTft_buff_time()));
						}
						if (!Util.isSpaceCheck(inTrx.getIary().get(j).getCf_buff_time())) {
							prdList.get(i).setC_rubbing_time(Integer.valueOf(inTrx
									.getIary().get(j).getCf_buff_time()));
						}
						if (!Util.isSpaceCheck(inTrx.getIary().get(j)
								.getTft_buff_tool())) {
							prdList.get(i).setFst_judge_tool_id(inTrx.getIary().get(j)
									.getTft_buff_tool());
							prdList.get(i).setFst_judge_timestamp(gEvtTimestamp);
						}
						if (!Util
								.isSpaceCheck(inTrx.getIary().get(j).getCf_buff_tool())) {
							prdList.get(i).setSnd_judge_tool_id(inTrx.getIary().get(i)
									.getCf_buff_tool());
							prdList.get(i).setSnd_judge_timestamp(gEvtTimestamp);
						}
					}
					// Save Prd_grade
					prdList.get(i).setPrd_grade(inTrx.getIary().get(j).getJudge_result());
					
					if(!Util.isSpaceCheck(inTrx.getIary().get(j).getMtrl_slot_no_fk())){
                    prdList.get(i).setMtrl_slot_no_fk(inTrx.getIary().get(j).getMtrl_slot_no_fk());
					}

					// Send QA HIS
					if (inTrx.getIary().get(j).getIsQaIns()) {
						prdList.get(i).setEvt_cate(EVT_QA_INSPECTION);
					}
					//Send retain HIS
					if (ret_box.getBnk_flg().equals(WIP_REATIN)) {// 在制保留的箱子，转进来的产品均将其BNK_FLG置为3
						prdList.get(i).setEvt_cate(EVT_WIP_RETAIN);
						prdList.get(i).setBnk_flg(WIP_REATIN);
					}	
					prdList.get(i).setEvt_cate(EVT_PNL_MOVE_OUT);
					break;
				}
			}
		}
		return new SimpleRtnBeen(prdList);
	}

	/**
	 * Get & chk box info
	 * 
	 * @param boxId
	 * @return
	 */
	private SimpleRtnBeen checkRetBox(String boxId) {
		Ret_box ret_box = new Ret_box();
		ret_box = retBoxDao.getWithLock(boxId, LockMode.UPGRADE);
		if (null == ret_box) {
			gszMsgBuf = new NotFoundRtnBeen(boxId, Ret_box.class).toString();
			return new SimpleRtnBeen(E_RET_BOX + E_READ_NOT_FOUND);
		}
		if (!PROC_EMPTY.equals(ret_box.getBox_stat())) {
			gszMsgBuf = "箱子 [" + ret_box.getBox_id() + "] " + "的状态为 ["
					+ ret_box.getBox_stat() + "], " + ",必须是" + PROC_EMPTY + "!";
			return new SimpleRtnBeen(E_XPPNLOUT_INVALID_BOX_STATUS);
		}

		if (!_YES.equals(ret_box.getValid_flg())) {
			gszMsgBuf = "箱子 [" + ret_box.getBox_id() + "] 无效!";
			return new SimpleRtnBeen(E_XPPNLOUT_BOX_INVALIED);
		}

		if (ret_box.getPrd_qty() == ret_box.getStd_qty()) {
			gszMsgBuf = "箱子 [" + ret_box.getBox_id() + "] 已满!";
			return new SimpleRtnBeen(E_XPPNLOUT_BOX_IS_FULL);
		}

		if (ret_box.getPrd_qty() + inTrx.getPnl_cnt() > ret_box.getStd_qty()) {
			gszMsgBuf = "箱子 [" + ret_box.getBox_id() + "] 当前的产品数量为["
					+ ret_box.getPrd_qty() + "], 转入数量为 [" + inTrx.getPnl_cnt()
					+ "],超出标准数量!";
			return new SimpleRtnBeen(E_XPPNLOUT_BOX_WILL_FULL);
		}

		if (0 == ret_box.getPrd_qty()) {
			if (!EMPTY_CARRIER.equals(ret_box.getReal_emp())) {
				gszMsgBuf = "箱子 [" + ret_box.getBox_id()
						+ "] 的产品数量为 0, 但箱子的real_emp非Y!";
				return new SimpleRtnBeen(E_XPPNLOUT_BOX_NOT_REAL_EMPT);
			}
		}

		return new SimpleRtnBeen(ret_box);
	}

	private long checkBoxMix() {
		long rtnCode = _NORMAL;
		Integer mode_cnt = 0;
		List<Ret_prd_info> ret_prd_infoList = new ArrayList<Ret_prd_info>();
		// Get prd info
		ret_prd_infoList = retPrdInfoDao.getShtListInfoWithLock(inTrx.getBox_id());
		if(ret_prd_infoList != null){
			for(int i=0; i<ret_prd_infoList.size();i++){
				//粒数统计改为直接累加prd_qty
				mode_cnt = mode_cnt + ret_prd_infoList.get(i).getPrd_qty();
				// Proc info
//				if("Y".equals(ret_prd_infoList.get(0).getOqc_skip_flg())){
//					String glassId=	ret_prd_infoList.get(0).getPrd_seq_id();
//					String hql = "FROM Ret_prd_out WHERE prd_seq_id_fk='"+glassId+"'";
//					List<Ret_prd_out> prd_list = retPrdOutDao.find(hql);
//					if (prd_list==null||prd_list.size()==0){
//						return _ERROR;
//					}else {
//					String  PathId=prd_list.get(0).getOut_path_id_fk();
//					String  PathVar=prd_list.get(0).getOut_path_ver_fk();
//					String  pathNo=prd_list.get(0).getOut_ope_no_fk();
//					String sql1= "FROM Bis_path WHERE path_id_fk='"+PathId+"' AND path_ver_fk='"+PathVar+"' AND cr_ope_no='"+pathNo+"'";
//					List<Bis_path> bispathlist = bispathDao.find(sql1);
//					String sql2= "FROM Bis_ope WHERE ope_id='"+bispathlist.get(0).getCr_ope_id_fk()+"'AND ope_ver='"+bispathlist.get(0).getCr_ope_ver_fk()+"'";
//					List<Bis_ope> bisOpeList=bisOpeDao.find(sql2);
//					if("N".equals(ret_prd_infoList.get(i).getOqc_skip_flg())){
//						if(!bisOpeList.get(0).getProc_id().equals(ret_prd_infoList.get(i).getNx_proc_id_fk())){
//							gszMsgBuf = "玻璃的下一制程不能混！[" + ret_prd_infoList.get(0).getPrd_seq_id() + "]["
//									+ ret_prd_infoList.get(0).getNx_proc_id_fk() + "] vs ["
//									+ ret_prd_infoList.get(i).getPrd_seq_id() + "]["
//									+ ret_prd_infoList.get(i).getNx_proc_id_fk() + "] ";
//						}
//					}else{
//						String glassId1=ret_prd_infoList.get(i).getPrd_seq_id();
//						String hql1 = "FROM Ret_prd_out WHERE prd_seq_id_fk='"+glassId1+"'";
//						List<Ret_prd_out> prd_list1 = retPrdOutDao.find(hql1);
//						if(prd_list1==null||prd_list1.size()==0){
//							return  _ERROR;
//						}else {
//							String  PathId1=prd_list1.get(0).getOut_path_id_fk();
//							String  PathVar1=prd_list1.get(0).getOut_path_ver_fk();
//							String  pathNo1=prd_list1.get(0).getOut_ope_no_fk();
//							String sql11= "FROM Bis_path WHERE path_id_fk='"+PathId1+"' AND path_ver_fk='"+PathVar1+"' AND cr_ope_no='"+pathNo1+"'";
//							List<Bis_path>	bispathlist1 = bispathDao.find(sql11);
//							String sql21= "FROM Bis_ope WHERE ope_id='"+bispathlist1.get(0).getCr_ope_id_fk()+"'AND ope_ver='"+bispathlist1.get(0).getCr_ope_ver_fk()+"'";
//							List<Bis_ope> bisOpeList1=bisOpeDao.find(sql21);
//							if(!bisOpeList.get(0).getProc_id().equals(bisOpeList1.get(0).getProc_id())){
//								gszMsgBuf = "玻璃的下一制程不能混！[" + ret_prd_infoList.get(0).getPrd_seq_id() + "]["
//										+ ret_prd_infoList.get(0).getNx_proc_id_fk() + "] vs ["
//										+ ret_prd_infoList.get(i).getPrd_seq_id() + "]["
//										+ ret_prd_infoList.get(i).getNx_proc_id_fk() + "] ";
//							}
//						}
//					  }
//					}
//				}else{
//					if("Y".equals(ret_prd_infoList.get(i).getOqc_skip_flg())){
//						String glassId=	ret_prd_infoList.get(0).getPrd_seq_id();
//						String hql = "FROM Ret_prd_out WHERE prd_seq_id_fk='"+glassId+"'";
//						List<Ret_prd_out> prd_list = retPrdOutDao.find(hql);
//						String  PathId=prd_list.get(0).getOut_path_id_fk();
//						String  PathVar=prd_list.get(0).getOut_path_ver_fk();
//						String  pathNo=prd_list.get(0).getOut_ope_no_fk();
//						List<Bis_path> bispathlist = new ArrayList<Bis_path>();
//						String sql1= "FROM Bis_path WHERE path_id_fk='"+PathId+"' AND path_ver_fk='"+PathVar+"' AND cr_ope_no='"+pathNo+"'";
//						bispathlist = bispathDao.find(sql1);
//						List<Bis_ope> bisOpeList = new ArrayList<Bis_ope>();
//						String sql2= "FROM Bis_ope WHERE ope_id='"+bispathlist.get(0).getCr_ope_id_fk()+"'AND ope_ver='"+bispathlist.get(0).getCr_ope_ver_fk()+"'";
//						bisOpeList=bisOpeDao.find(sql2);
//						if(!bisOpeList.get(0).getProc_id().equals(ret_prd_infoList.get(0).getNx_proc_id_fk())){
//							gszMsgBuf = "玻璃的下一制程不能混！[" + ret_prd_infoList.get(0).getPrd_seq_id() + "]["
//									+ ret_prd_infoList.get(0).getNx_proc_id_fk() + "] vs ["
//									+ ret_prd_infoList.get(i).getPrd_seq_id() + "]["
//									+ ret_prd_infoList.get(i).getNx_proc_id_fk() + "] ";
//						}
//					}else {
						if (!ret_prd_infoList.get(0).getNx_proc_id_fk().equals(
								ret_prd_infoList.get(i).getNx_proc_id_fk())) {
							gszMsgBuf = "玻璃的下一制程不能混！[" + ret_prd_infoList.get(0).getPrd_seq_id() + "]["
									+ ret_prd_infoList.get(0).getNx_proc_id_fk() + "] vs ["
									+ ret_prd_infoList.get(i).getPrd_seq_id() + "]["
									+ ret_prd_infoList.get(i).getNx_proc_id_fk() + "] ";
						}
//					}
//				}
//				if (!ret_prd_infoList.get(0).getNx_proc_id_fk().equals(
//						ret_prd_infoList.get(i).getNx_proc_id_fk())) {
//                      if("Y".equals(ret_prd_infoList.get(0).getOqc_skip_flg())){
//                        String glassId=	ret_prd_infoList.get(0).getPrd_seq_id();
//						  String hql = "FROM Ret_prd_out WHERE prd_seq_id_fk='"+glassId+"'";
//						  List<Ret_prd_out> prd_list = retPrdOutDao.find(hql);
//						 String  PathId=prd_list.get(0).getOut_path_id_fk();
//						 String  PathVar=prd_list.get(0).getOut_path_ver_fk();
//						 String  pathNo=prd_list.get(0).getOut_ope_no_fk();
//						  List<Bis_path> bispathlist = new ArrayList<Bis_path>();
//                           String sql1= "FROM Bis_path WHERE path_id_fk='"+PathId+"' AND path_ver_fk='"+PathVar+"' AND cr_ope_no='"+pathNo+"'";
//						                 bispathlist = bispathDao.find(sql1);
//						             List<Bis_ope> bisOpeList = new ArrayList<Bis_ope>();
//                           String sql2= "FROM Bis_ope WHERE ope_id='"+bispathlist.get(0).getCr_ope_id_fk()+"'AND ope_ver='"+bispathlist.get(0).getCr_ope_ver_fk()+"'";
//                                     bisOpeList=bisOpeDao.find(sql2);
//						  if(!bisOpeList.get(0).getProc_id().equals(ret_prd_infoList.get(0).getNx_proc_id_fk())){
//							  gszMsgBuf = "玻璃的下一制程不能混！[" + ret_prd_infoList.get(0).getPrd_seq_id() + "]["
//									  + ret_prd_infoList.get(0).getNx_proc_id_fk() + "] vs ["
//									  + ret_prd_infoList.get(i).getPrd_seq_id() + "]["
//									  + ret_prd_infoList.get(i).getNx_proc_id_fk() + "] ";
//						  }
//				      }else{
//						  gszMsgBuf = "玻璃的下一制程不能混！ [" + ret_prd_infoList.get(0).getPrd_seq_id() + "]["
//								  + ret_prd_infoList.get(0).getNx_proc_id_fk() + "] vs ["
//								  + ret_prd_infoList.get(i).getPrd_seq_id() + "]["
//								  + ret_prd_infoList.get(i).getNx_proc_id_fk() + "] ";
//						  return SHT_NX_PROC_ID_MIX;
//					  }
//				}
				if (!ret_prd_infoList.get(0).getCr_proc_id_fk().equals(
						ret_prd_infoList.get(i).getCr_proc_id_fk())) {
					gszMsgBuf = "玻璃的当前制程不能混！[" + ret_prd_infoList.get(0).getPrd_seq_id() + "]["
							+ ret_prd_infoList.get(0).getCr_proc_id_fk() + "] vs ["
							+ ret_prd_infoList.get(i).getPrd_seq_id() + "]["
							+ ret_prd_infoList.get(i).getCr_proc_id_fk() + "] ";
					return SHT_CR_PROC_ID_MIX;
				}

				// MDL info
				if (!ret_prd_infoList.get(0).getMdl_cate_fk().equals(
						ret_prd_infoList.get(i).getMdl_cate_fk())) {
					gszMsgBuf = "玻璃的产品类型不能混！ [" + ret_prd_infoList.get(0).getPrd_seq_id() + "]["
							+ ret_prd_infoList.get(0).getMdl_cate_fk() + "] vs ["
							+ ret_prd_infoList.get(i).getPrd_seq_id() + "]["
							+ ret_prd_infoList.get(i).getMdl_cate_fk() + "] ";
					return SHT_MDL_CATE_MIX;
				}
				if (!ret_prd_infoList.get(0).getMdl_id_fk().equals(
						ret_prd_infoList.get(i).getMdl_id_fk())) {
					gszMsgBuf = "玻璃的产品ID不能混！[" + ret_prd_infoList.get(0).getPrd_seq_id() + "]["
							+ ret_prd_infoList.get(0).getMdl_id_fk() + "] vs ["
							+ ret_prd_infoList.get(i).getPrd_seq_id() + "]["
							+ ret_prd_infoList.get(i).getMdl_id_fk() + "] ";
					return SHT_MDL_ID_MIX;
				}
				if (!ret_prd_infoList.get(0).getMdl_typ_fk().equals(
						ret_prd_infoList.get(i).getMdl_typ_fk())) {
					gszMsgBuf = "玻璃的产品尺寸不能混！ [" + ret_prd_infoList.get(0).getPrd_seq_id() + "]["
							+ ret_prd_infoList.get(0).getMdl_typ_fk() + "] vs ["
							+ ret_prd_infoList.get(i).getPrd_seq_id() + "]["
							+ ret_prd_infoList.get(i).getMdl_typ_fk() + "] ";
					return SHT_MDL_TYPE_MIX;
				}

				// Path in
				if (!ret_prd_infoList.get(0).getNx_path_id_fk().equals(
						ret_prd_infoList.get(i).getNx_path_id_fk())) {
					gszMsgBuf = "玻璃的下一工艺路线不能混！[" + ret_prd_infoList.get(0).getPrd_seq_id()
							+ "][" + ret_prd_infoList.get(0).getNx_path_id_fk() + "] vs ["
							+ ret_prd_infoList.get(i).getPrd_seq_id() + "]["
							+ ret_prd_infoList.get(i).getNx_path_id_fk() + "] ";
					return SHT_PATH_ID_MIX;
				}
				if (!ret_prd_infoList.get(0).getNx_path_ver_fk().equals(
						ret_prd_infoList.get(i).getNx_path_ver_fk())) {
					gszMsgBuf = "玻璃的下一工艺路线版本不能混！ [" + ret_prd_infoList.get(0).getPrd_seq_id()
							+ "][" + ret_prd_infoList.get(0).getNx_path_ver_fk() + "] vs ["
							+ ret_prd_infoList.get(i).getPrd_seq_id() + "]["
							+ ret_prd_infoList.get(i).getNx_path_ver_fk() + "] ";
					return SHT_PATH_VER_MIX;
				}
				if (!ret_prd_infoList.get(0).getNx_ope_id_fk().equals(
						ret_prd_infoList.get(i).getNx_ope_id_fk())) {
					gszMsgBuf = "玻璃的下一站点ID不能混！ [" + ret_prd_infoList.get(0).getPrd_seq_id()
							+ "][" + ret_prd_infoList.get(0).getNx_ope_id_fk() + "] vs ["
							+ ret_prd_infoList.get(i).getPrd_seq_id() + "]["
							+ ret_prd_infoList.get(i).getNx_ope_id_fk() + "] ";
					return SHT_OPE_ID_MIX;
				}
				if (!ret_prd_infoList.get(0).getNx_ope_no_fk().equals(
						ret_prd_infoList.get(i).getNx_ope_no_fk())) {
					gszMsgBuf = "玻璃的下一站点编号不能混！ [" + ret_prd_infoList.get(0).getPrd_seq_id()
							+ "][" + ret_prd_infoList.get(0).getNx_ope_no_fk() + "] vs ["
							+ ret_prd_infoList.get(i).getPrd_seq_id() + "]["
							+ ret_prd_infoList.get(i).getNx_ope_no_fk() + "] ";
					return SHT_OPE_NO_MIX;
				}
				if (!ret_prd_infoList.get(0).getNx_ope_ver_fk().equals(
						ret_prd_infoList.get(i).getNx_ope_ver_fk())) {
					gszMsgBuf = "玻璃的下一站点版本不能混！[" + ret_prd_infoList.get(0).getPrd_seq_id()
							+ "][" + ret_prd_infoList.get(0).getNx_ope_ver_fk() + "] vs ["
							+ ret_prd_infoList.get(i).getPrd_seq_id() + "]["
							+ ret_prd_infoList.get(i).getNx_ope_ver_fk() + "] ";
					return SHT_OPE_VER_MIX;
				}

				// Tool info
				if (!ret_prd_infoList.get(0).getNx_toolg_id_fk().equals(
						ret_prd_infoList.get(i).getNx_toolg_id_fk())) {
					gszMsgBuf = "玻璃的设备组不能混！[" + ret_prd_infoList.get(0).getPrd_seq_id() + "]["
							+ ret_prd_infoList.get(0).getNx_toolg_id_fk() + "] vs ["
							+ ret_prd_infoList.get(i).getPrd_seq_id() + "]["
							+ ret_prd_infoList.get(i).getNx_toolg_id_fk() + "] ";
					return SHT_NX_TOOLG_MIX;
				}

				// WO info
				if (!ret_prd_infoList.get(0).getWo_id_fk().equals(
						ret_prd_infoList.get(i).getWo_id_fk())) {
					gszMsgBuf = "玻璃的内部订单不能混！ [" + ret_prd_infoList.get(0).getPrd_seq_id() + "]["
							+ ret_prd_infoList.get(0).getWo_id_fk() + "] vs ["
							+ ret_prd_infoList.get(i).getPrd_seq_id() + "]["
							+ ret_prd_infoList.get(i).getWo_id_fk() + "] ";
					return SHT_WO_ID_MIX;
				}
				
				//GROUP ID,针对客户来料的订单，不同group id(值来自RET_CUS_PRD_IN的CUS_GROUP_ID,即owner id)的产品不能混装
				Ret_wo ret_wo = retWoDao.getWithLock(ret_prd_infoList.get(0).getWo_id_fk(), LockMode.UPGRADE);
				if (null == ret_wo) {
					gszMsgBuf = "内部订单 [" + ret_prd_infoList.get(0).getWo_id_fk() + "] 不存在!";
					return E_RET_WO + E_READ_NOT_FOUND;
				}
				
				//检查物理子格位是否相同,只针对某些客户
//				if("Y".equals(ret_wo.getOpe_eff_flg())){
//					for(int j=i+1; j<ret_prd_infoList.size();j++){
//						if(!Util.isSpaceCheck(ret_prd_infoList.get(i).getSlot_no())){
//							if(ret_prd_infoList.get(i).getSlot_no().equals(ret_prd_infoList.get(j).getSlot_no())){
//								gszMsgBuf = "玻璃的实格位不能相同！ [" + ret_prd_infoList.get(i).getPrd_seq_id() + "]["
//									+ ret_prd_infoList.get(i).getSlot_no() + "] vs ["
//									+ ret_prd_infoList.get(j).getPrd_seq_id() + "]["
//									+ ret_prd_infoList.get(j).getSlot_no() + "] ";
//								return SHT_MTRL_SLOT_NO_FK_SAME;
//							}
//						}else{
//							break;
//						}
//					}
//				}
				
//				if(_YES.equals(inTrx.getGchk_flg())){//客户来料标记
//					if(!ret_prd_infoList.get(0).getGroup_id().equals(ret_prd_infoList.get(i).getGroup_id())){
//						gszMsgBuf = "玻璃的组代码不能混！[" + ret_prd_infoList.get(0).getPrd_seq_id()+ "]["
//							+ ret_prd_infoList.get(0).getGroup_id() + "] vs ["
//							+ ret_prd_infoList.get(i).getPrd_seq_id() + "]["
//							+ ret_prd_infoList.get(i).getGroup_id() + "]";
//						return GROUP_ID_MIX;
//					}
//					if(_YES.equals(ret_wo.getSwh_cnt_flg())||"007".equals(ret_wo.getCus_id_fk())){
//						if(!ret_prd_infoList.get(0).getPrd_grade().equals(ret_prd_infoList.get(i).getPrd_grade())){
//							gszMsgBuf = "玻璃的等级不能混！[" + ret_prd_infoList.get(0).getPrd_seq_id()+ "]["
//								+ ret_prd_infoList.get(0).getPrd_grade() + "] vs ["
//								+ ret_prd_infoList.get(i).getPrd_seq_id() + "]["
//								+ ret_prd_infoList.get(i).getPrd_grade() + "]";
//							return GROUP_ID_MIX;
//						}
//					}
//				}
			}
		}
		outTrx.setMode_cnt(mode_cnt);
		return rtnCode;
	}

	private String getSlotNo(){
		String slot_no="";
		String sql = "SELECT SLOT_NO FROM Ret_prd_info WHERE box_id_fk='"+inTrx.getBox_id()+"' ORDER BY  CAST(SLOT_NO AS SIGNED)";
		List list = retPrdInfoDao.findBySQL(sql);
		List<Integer> slots = new ArrayList<Integer>();
		for(int i = 0; i < list.size(); i++){
		     slots.add(Integer.parseInt((String)list.get(i)));
		}
		int tempSlot = 0;
		for(int i = 1; ; i++){
			if(!slots.contains(i)){
				tempSlot = i;
				break;
			}
		}
		slot_no = tempSlot + "";
		return slot_no;
	}

//	private String getSlotNo1(){
//		String slot_no="";
//		String sql = "SELECT SLOT_NO FROM Ret_prd_info WHERE box_id_fk='"+inTrx.getBox_id()+"' ORDER BY  CAST(SLOT_NO AS SIGNED)";
//		List list = retPrdInfoDao.findBySQL(sql);
//		List<Integer> slots = new ArrayList<Integer>();
//		slot_no =String .valueOf(list.size()+1) ;
//		return slot_no;
//	}


	public static void main(String[] args) {
		System.out.println(Util.getTimestampByDblTime(43132.91388901621));;
	}


}
